﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VeryCodes.Common;
using System.Web.UI.HtmlControls;
using System.Collections;

namespace VeryCodes.Controls.Web
{
    /// <summary>
    /// 数据树,来源于JavaScript DTree
    /// 需要绑定数据源，设置相关字段
    /// </summary>
    [DefaultProperty("ShowCheckBox")]
    [ToolboxData("<{0}:DataTree runat=server></{0}:DataTree>")]
    [Designer(typeof(VeryCodes.Controls.Web.CommonDesigner))]
    public class DataTree : CompositeControl
    {
        private bool _ShowCheckBox = false; //是否启动CheckBox模式
        private bool _RelationParent = false; //选中下级是否同时选中上级，在启用CheckBox模式情况下
        private bool _AllOpen = false;//全部展开
        private bool _UseIcons = true;//显示图标
        private bool _UseLines = true;//显示结构线

        private IList<String> _JsList = null; //Js文件列表
        private IList<String> _StyleSheetList = null; //样式表列表

        private string _TreeStyleFile = string.Empty; //树的样式表
        private string _TreeStyleId = "dtree"; //树的样式所属Id
        private string _RootNodePK = string.Empty; //根节点PK，用于在初始数据中匹配上下级
        private string _RootNodeCode = string.Empty; //根节点编码，用于在javascript中匹配上下级，为空与_RootNodePK相同
        private string _RootNodeName = "根节点"; //根节点名称，默认为"根节点"
        private string _NodeValueField = string.Empty; //节点值字段
        private string _NodeTextFiled = string.Empty; //节点文本字段
        private string _NodeRelationField = string.Empty; //构建JavaScript上下级节点关联的字段,为空与_NodePKField相同
        private string _NodePKField = string.Empty; //上下级关联的字段
        private string _ParentFiled = string.Empty; //用于数据初始化设置父节点的字段
        private string _DescriptionFiled = string.Empty; //要显示的描述字段
        private string _DescriptionTitle = "简介"; //描述文本的标题
        private string _NodeUrl = ""; //点击节点触发事件，可以为链接或JavaScript
        private string _NodeTarget = ""; //链接显示位置
        private string _NodeTargetFiled = ""; //链接显示位置字段
        private string _CheckedNodeText = string.Empty;//选中的节点文本
        private string _CheckedNodeValue = string.Empty;//选中的节点值
        private string _CheckedNodeField = string.Empty;//获取是否选择节点的字段
        private string _NodeIconField = string.Empty;//节点图标的字段
        private string _NodeIconOpenField = string.Empty;//打开节点图标的字段
        private string _NodeIconPath = string.Empty;//图标的路径
        private string _EnableCheckValue = string.Empty;//支持单选框的值，只有单选框启动时
        private DataTable _BindDataTable; //绑定数据

        #region 重写方法

        /// <summary>
        /// 重写RenderContents
        /// 在页面上输出DTree的节点信息
        /// </summary>
        /// <param name="output"></param>
        protected override void RenderContents(HtmlTextWriter output)
        {
            RenderTree(output);
        }

        /// <summary>
        /// 重写OnPreRender
        /// 注册样式表文件和Js文件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            //注册样式表文件
            RegStyleSheet();

            //注册Javascript文件
            RegJavaScript();
        }

        #endregion

        #region 公共属性

        /// <summary>
        /// Js文件列表
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("Js文件列表")]
        public IList<String> StyleSheetList
        {
            get
            {
                if (_StyleSheetList == null)
                {
                    _StyleSheetList = new List<string>();
                }
                return _StyleSheetList;
            }
            set
            {
                _StyleSheetList = value;

            }
        }

        /// <summary>
        /// Js文件列表
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("Js文件列表")]
        public IList<String> JsList
        {
            get
            {
                if (_JsList == null)
                {
                    _JsList = new List<string>();
                }
                return _JsList;
            }
            set
            {
                _JsList = value;

            }
        }

        /// <summary>
        /// 设置是否显示节点图标
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("设置是否显示节点图标")]
        public bool UseIcons
        {
            get
            {
                return _UseIcons;
            }
            set
            {
                _UseIcons = value;
            }
        }

        /// <summary>
        /// 设置是否显示结构连线
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("设置是否显示结构连线")]
        public bool UseLines
        {
            get
            {
                return _UseLines;
            }
            set
            {
                _UseLines = value;
            }
        }

        /// <summary>
        /// 设置选中的节点文本
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("设置选中的节点的文本，只在ShowCheckBox=true才有效")]
        public string CheckedNodeText
        {
            get
            {
                return _CheckedNodeText;
            }
            set
            {
                _CheckedNodeText = value;
            }

        }

        /// <summary>
        /// 设置选中的节点值
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("设置选中的节点的值，只在ShowCheckBox=true才有效")]
        public string CheckedNodeValue
        {
            get
            {
                return _CheckedNodeValue;
            }
            set
            {
                _CheckedNodeValue = value;
            }

        }

        /// <summary>
        /// 支持单选框的节点值，只在ShowCheckBox=true才有效
        /// </summary>
        [Browsable(false)]
        [Category("DataTree")]
        [Description("支持单选框的节点值，只在ShowCheckBox=true才有效")]
        public string EnableCheckValue
        {
            get
            {
                return _EnableCheckValue;
            }
            set
            {
                _EnableCheckValue = value;
            }

        }

        /// <summary>
        /// 设置选中的节点的字段
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("设置选中的节点的字段，只在ShowCheckBox=true时才有效，优先级高于SelectedNode")]
        public string CheckedNodeField
        {
            get
            {
                return _CheckedNodeField;
            }
            set
            {
                _CheckedNodeField = value;

            }

        }

        /// <summary>
        /// 设置节点链接指向位置
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("设置节点链接指向位置")]
        public string NodeTargetField
        {
            get
            {
                return _NodeTargetFiled;
            }
            set
            {
                _NodeTargetFiled = value;

            }

        }

        /// <summary>
        /// 图标的路径
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("图标的路径")]
        public string NodeIconPath
        {
            get
            {
                return _NodeIconPath;
            }
            set
            {
                _NodeIconPath = value;

            }
        }

        /// <summary>
        /// 节点图标的字段
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("设置节点图标的字段")]
        public string NodeIconField
        {
            get
            {
                return _NodeIconField;
            }
            set
            {
                _NodeIconField = value;

            }
        }

        /// <summary>
        /// 节点打开图标的字段
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("设置节点打开图标的字段")]
        public string NodeIconOpenField
        {
            get
            {
                return _NodeIconOpenField;
            }
            set
            {
                _NodeIconOpenField = value;

            }
        }

        /// <summary>
        /// 树的样式所属Id，必须和样式表文件同时设置才有效，默认为DTree标准样式表Id
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("树的样式所属Id，必须和样式表文件同时设置才有效，默认为DTree标准样式表Id")]
        public string TreeStyleId
        {
            get
            {
                return _TreeStyleId;
            }
            set
            {
                _TreeStyleId = value;

            }

        }

        /// <summary>
        /// 树的样式表文件，必须和树的样式所属Id同时设置才有效，默认为DTree标准样式表
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("树的样式表文件，必须和树的样式所属Id同时设置才有效，默认为DTree标准样式表")]
        public string TreeStyleFile
        {
            get
            {
                return _TreeStyleFile;
            }
            set
            {
                _TreeStyleFile = value;

            }

        }

        /// <summary>
        /// 描述文本的标题，默认为：简介
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("描述文本的标题，默认为：简介")]
        public string DescriptionTitle
        {
            get
            {
                return _DescriptionTitle;
            }
            set
            {
                _DescriptionTitle = value;

            }

        }

        /// <summary>
        /// 描述字段，可以设置多个，可以为空，形式为：字段一|field1,字段二|filed2
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("描述字段，可以设置多个，可以为空，形式为：字段一|field1,字段二|filed2")]
        public string DescriptionFiled
        {
            get
            {
                return _DescriptionFiled;
            }
            set
            {
                _DescriptionFiled = value;

            }

        }

        /// <summary>
        /// 节点值字段，必须设置
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("节点值字段，必须设置")]
        public string NodeValueField
        {
            get
            {
                return _NodeValueField;
            }
            set
            {
                _NodeValueField = value;

            }

        }

        /// <summary>
        /// 节点显示文本字段，必须设置
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("节点显示文本字段，必须设置")]
        public string NodeTextFiled
        {
            get
            {
                return _NodeTextFiled;
            }
            set
            {
                _NodeTextFiled = value;

            }

        }

        /// <summary>
        /// 用于上下级节点关联信息的字段，用于javascript,默认与NodePKField相同
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("用于上下级节点关联信息的字段，用于javascript,默认与NodePKField相同")]
        public string NodeRelationField
        {
            get
            {
                if (_NodeRelationField == string.Empty)
                {
                    _NodeRelationField = NodePKField;
                }
                return _NodeRelationField;
            }
            set
            {
                _NodeRelationField = value;

            }

        }

        /// <summary>
        /// 用于上下级节点关联信息的字段，必须设置
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("用于上下级节点关联信息的字段，用于数据初始化，必须设置")]
        public string NodePKField
        {
            get
            {
                return _NodePKField;
            }
            set
            {
                _NodePKField = value;
            }
        }

        /// <summary>
        /// 存放父节点信息的字段,用于查找子节点，必须设置
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("存放父节点信息的字段,用于查找子节点，必须设置")]
        public string ParentFiled
        {
            get
            {
                return _ParentFiled;
            }
            set
            {
                _ParentFiled = value;

            }

        }


        /// <summary>
        /// 根节点PK,最上级的父节点，必须设置
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("根节点PK,最上级的父节点，必须设置")]
        public string RootNodePK
        {
            get
            {
                return _RootNodePK;
            }
            set
            {
                _RootNodePK = value;

            }
        }

        /// <summary>
        /// 根节点Code,最上级的父节点
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("根节点Code,最上级的父节点，默认与RootNodePK属性相同")]
        public string RootNodeCode
        {
            get
            {
                if (_RootNodeCode == string.Empty)
                {
                    _RootNodeCode = _RootNodePK;
                }
                return _RootNodeCode;
            }
            set
            {
                _RootNodeCode = value;

            }
        }

        /// <summary>
        /// 根节点显示名称，默认为：根节点
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("根节点显示名称，默认为：根节点")]
        public string RootNodeName
        {
            get
            {
                return _RootNodeName;
            }
            set
            {
                _RootNodeName = value;

            }
        }

        /// <summary>
        /// 是否使用CheckBox模式
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("是否使用CheckBox模式，默认为false")]
        public bool ShowCheckBox
        {
            get
            {
                return _ShowCheckBox;
            }

            set
            {
                _ShowCheckBox = value;
            }
        }

        /// <summary>
        /// 选中下级是否同时选中上级
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("选中下级是否同时选中上级，默认为false")]
        public bool RelationParent
        {
            get
            {
                return _RelationParent;
            }

            set
            {
                _RelationParent = value;
            }
        }

        /// <summary>
        /// 是否全部展开
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("是否全部展开，默认为false")]
        public bool AllOpen
        {
            get
            {
                return _AllOpen;
            }

            set
            {
                _AllOpen = value;
            }
        }

        /// <summary>
        /// 点击节点的链接显示位置：target
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("点击节点的链接显示位置：target")]
        public string NodeTarget
        {
            get
            {
                return _NodeTarget;
            }

            set
            {
                _NodeTarget = value;
            }
        }

        /// <summary>
        /// 点击节点的链接或事件
        /// </summary>
        [Browsable(true)]
        [Category("DataTree")]
        [Description("点击节点的链接或事件，默认为获取该节点信息")]
        public string NodeUrl
        {
            get
            {
                return _NodeUrl;
            }

            set
            {
                _NodeUrl = value;
            }
        }

        /// <summary>
        /// 绑定DataTable数据源
        /// </summary>
        [Browsable(true)]
        [Description("绑定DataTable数据源")]
        public DataTable BindDataTable
        {
            get
            {
                return _BindDataTable;
            }
            set
            {
                _BindDataTable = value;
            }
        }

        #endregion

        #region 用到的图片

        private string m_DTreeRootImage = string.Empty;
        /// <summary>
        /// 树元素图片root.gif
        /// </summary>
        public string DTreeRootImage
        {
            get
            {
                if (m_DTreeRootImage == string.Empty)
                {
                    return Page.ClientScript.GetWebResourceUrl(this.GetType(), "VeryCodes.Controls.Web.DTree.root.gif");
                }
                return m_DTreeRootImage;
            }

            set
            {
                m_DTreeRootImage = value;
            }
        }

        private string m_DTreeFolderImage = string.Empty;
        /// <summary>
        /// 树元素图片folder.gif
        /// </summary>
        public string DTreeFolderImage
        {
            get
            {
                if (m_DTreeFolderImage == string.Empty)
                {
                    return Page.ClientScript.GetWebResourceUrl(this.GetType(), "VeryCodes.Controls.Web.DTree.folder.gif");
                }
                return m_DTreeFolderImage;
            }

            set
            {
                m_DTreeFolderImage = value;
            }
        }

        private string m_DTreeFolderOpenImage = string.Empty;
        /// <summary>
        /// 树元素图片folderopen.gif
        /// </summary>
        public string DTreeFolderOpenImage
        {
            get
            {
                if (m_DTreeFolderOpenImage == string.Empty)
                {
                    return Page.ClientScript.GetWebResourceUrl(this.GetType(), "VeryCodes.Controls.Web.DTree.folderopen.gif");
                }
                return m_DTreeFolderOpenImage;
            }

            set
            {
                m_DTreeFolderOpenImage = value;
            }
        }

        private string m_DTreeNodeImage = string.Empty;
        /// <summary>
        /// 树元素图片node.gif
        /// </summary>
        public string DTreeNodeImage
        {
            get
            {
                if (m_DTreeNodeImage == string.Empty)
                {
                    return Page.ClientScript.GetWebResourceUrl(this.GetType(), "VeryCodes.Controls.Web.DTree.node.gif");
                }
                return m_DTreeNodeImage;
            }

            set
            {
                m_DTreeNodeImage = value;
            }
        }


        private string m_DTreeJoinImage = string.Empty;
        /// <summary>
        /// 树元素图片join.gif
        /// </summary>
        public string DTreeJoinImage
        {
            get
            {
                if (m_DTreeJoinImage == string.Empty)
                {
                    return Page.ClientScript.GetWebResourceUrl(this.GetType(), "VeryCodes.Controls.Web.DTree.join.gif");
                }
                return m_DTreeJoinImage;
            }

            set
            {
                m_DTreeJoinImage = value;
            }
        }

        private string m_DTreeJoinBottomImage = string.Empty;
        /// <summary>
        /// 树元素图片joinbottom.gif
        /// </summary>
        public string DTreeJoinBottomImage
        {
            get
            {
                if (m_DTreeJoinBottomImage == string.Empty)
                {
                    return Page.ClientScript.GetWebResourceUrl(this.GetType(), "VeryCodes.Controls.Web.DTree.joinbottom.gif");
                }
                return m_DTreeJoinBottomImage;
            }

            set
            {
                m_DTreeJoinBottomImage = value;
            }
        }

        private string m_DTreeLineImage = string.Empty;
        /// <summary>
        /// 树元素图片line.gif
        /// </summary>
        public string DTreeLineImage
        {
            get
            {
                if (m_DTreeLineImage == string.Empty)
                {
                    return Page.ClientScript.GetWebResourceUrl(this.GetType(), "VeryCodes.Controls.Web.DTree.line.gif");
                }
                return m_DTreeLineImage;
            }

            set
            {
                m_DTreeLineImage = value;
            }
        }

        private string m_DTreeMinusImage = string.Empty;
        /// <summary>
        /// 树元素图片minus.gif
        /// </summary>
        public string DTreeMinusImage
        {
            get
            {
                if (m_DTreeMinusImage == string.Empty)
                {
                    return Page.ClientScript.GetWebResourceUrl(this.GetType(), "VeryCodes.Controls.Web.DTree.minus.gif");
                }
                return m_DTreeMinusImage;
            }

            set
            {
                m_DTreeMinusImage = value;
            }
        }

        private string m_DTreeMinusBottomImage = string.Empty;
        /// <summary>
        /// 树元素图片minusbottom.gif
        /// </summary>
        public string DTreeMinusBottomImage
        {
            get
            {
                if (m_DTreeMinusBottomImage == string.Empty)
                {
                    return Page.ClientScript.GetWebResourceUrl(this.GetType(), "VeryCodes.Controls.Web.DTree.minusbottom.gif");
                }
                return m_DTreeMinusBottomImage;
            }

            set
            {
                m_DTreeMinusBottomImage = value;
            }
        }

        private string m_DTreeNoLinesMinusImage = string.Empty;
        /// <summary>
        /// 树元素图片nolines_minus.gif
        /// </summary>
        public string DTreeNoLinesMinusImage
        {
            get
            {
                if (m_DTreeNoLinesMinusImage == string.Empty)
                {
                    return Page.ClientScript.GetWebResourceUrl(this.GetType(), "VeryCodes.Controls.Web.DTree.nolines_minus.gif");
                }
                return m_DTreeNoLinesMinusImage;
            }

            set
            {
                m_DTreeNoLinesMinusImage = value;
            }
        }

        private string m_DTreeNoLinesPlusImage = string.Empty;
        /// <summary>
        /// 树元素图片nolines_plus.gif
        /// </summary>
        public string DTreeNoLinesPlusImage
        {
            get
            {
                if (m_DTreeNoLinesPlusImage == string.Empty)
                {
                    return Page.ClientScript.GetWebResourceUrl(this.GetType(), "VeryCodes.Controls.Web.DTree.nolines_plus.gif");
                }
                return m_DTreeNoLinesPlusImage;
            }

            set
            {
                m_DTreeNoLinesPlusImage = value;
            }
        }

        private string m_DTreePlusImage = string.Empty;
        /// <summary>
        /// 树元素图片plus.gif
        /// </summary>
        public string DTreePlusImage
        {
            get
            {
                if (m_DTreePlusImage == string.Empty)
                {
                    return Page.ClientScript.GetWebResourceUrl(this.GetType(), "VeryCodes.Controls.Web.DTree.plus.gif");
                }
                return m_DTreePlusImage;
            }

            set
            {
                m_DTreePlusImage = value;
            }
        }

        private string m_DTreePlusBottomImage = string.Empty;
        /// <summary>
        /// 树元素图片plusbottom.gif
        /// </summary>
        public string DTreePlusBottomImage
        {
            get
            {
                if (m_DTreePlusBottomImage == string.Empty)
                {
                    return Page.ClientScript.GetWebResourceUrl(this.GetType(), "VeryCodes.Controls.Web.DTree.plusbottom.gif");
                }
                return m_DTreePlusBottomImage;
            }

            set
            {
                m_DTreePlusBottomImage = value;
            }
        }

        private string m_DTreeEmptyImage = string.Empty;
        /// <summary>
        /// 树元素图片empty.gif
        /// </summary>
        public string DTreeEmptyImage
        {
            get
            {
                if (m_DTreeEmptyImage == string.Empty)
                {
                    return Page.ClientScript.GetWebResourceUrl(this.GetType(), "VeryCodes.Controls.Web.DTree.empty.gif");
                }
                return m_DTreeEmptyImage;
            }

            set
            {
                m_DTreeEmptyImage = value;
            }
        }
        #endregion

        #region 私有属性及方法

        /// <summary>
        /// 返回Js文件的路径
        /// </summary>
        private string DTreeJsUrl
        {
            get
            {
                if (ViewState["JsUrl"] == null || ViewState["JsUrl"].ToString() == string.Empty)
                {
                    ViewState["JsUrl"] = Page.ClientScript.GetWebResourceUrl(this.GetType(), "VeryCodes.Controls.Web.DTree.dtree.js");
                }
                return ViewState["JsUrl"].ToString();
            }
        }

        /// <summary>
        /// 返回MouseTipsJs文件的路径
        /// </summary>
        private string MouseTipsJsUrl
        {
            get
            {
                if (ViewState["MouseTipsJsUrl"] == null || ViewState["MouseTipsJsUrl"].ToString() == string.Empty)
                {
                    ViewState["MouseTipsJsUrl"] = Page.ClientScript.GetWebResourceUrl(this.GetType(), "VeryCodes.Controls.Web.MouseTips.MouseTips.js");
                }
                return ViewState["MouseTipsJsUrl"].ToString();
            }
        }

        /// <summary>
        /// 注册JavaScript文件
        /// </summary>
        private void RegJavaScript()
        {
            InitJsList();

            int JsListCount = JsList.Count;
            for (int k = 0; k <= JsListCount - 1; k++)
            {
                Page.Header.Controls.Add(new LiteralControl(JsList[k] + "\r\n"));
            }
        }

        /// <summary>
        /// 初始化Js文件列表
        /// </summary>
        private void InitJsList()
        {
            //如果描述字段不为空，则注册显示描述文本所需JavaSript
            if (DescriptionFiled != string.Empty)
            {
                string TempJsUrl = "<script type=\"text/javascript\" src=\"" + MouseTipsJsUrl + "\" charset=\"gb2312\"></script>";
                if (!CheckContainFile.CheckContainJsFile(TempJsUrl, this.Page))
                {
                    JsList.Add(TempJsUrl);
                }
            }

            //注册DTree必需JavaScript
            string TempJsUrl2 = "<script type=\"text/javascript\" src=\"" + DTreeJsUrl + "\"></script>";
            if (!CheckContainFile.CheckContainJsFile(DTreeJsUrl, this.Page))
            {
                JsList.Add(TempJsUrl2);
            }
        }

        /// <summary>
        /// 注册样式表文件
        /// </summary>
        private void RegStyleSheet()
        {
            InitStyleSheetList();

            int StyleSheetListCount = StyleSheetList.Count;
            for (int k = 0; k <= StyleSheetListCount - 1; k++)
            {
                HtmlLink cssLink = new HtmlLink();
                cssLink.Href = StyleSheetList[k];
                cssLink.Attributes.Add("rel", "stylesheet");
                cssLink.Attributes.Add("type", "text/css");
                this.Page.Header.Controls.Add(cssLink);
                Page.Header.Controls.Add(new LiteralControl("\r\n"));
            }

        }

        /// <summary>
        /// 初始化样式表文件列表
        /// </summary>
        private void InitStyleSheetList()
        {
            string cssUrl = GetStyleSheetUrl();

            //检查是否已经包含相同的Css文件
            if (!CheckContainFile.CheckContainStyle(cssUrl, this.Page))
            {
                StyleSheetList.Add(cssUrl);
            }

            if (DescriptionFiled != string.Empty)
            {
                string cssUrl2 = Page.ClientScript.GetWebResourceUrl(this.GetType(), "VeryCodes.Controls.Web.MouseTips.MouseTips.css");
                if (!CheckContainFile.CheckContainStyle(cssUrl2, this.Page))
                {
                    StyleSheetList.Add(cssUrl2);
                }
            }
        }

        /// <summary>
        /// 获取样式表文件
        /// </summary>
        /// <returns>样式表文件</returns>
        private string GetStyleSheetUrl()
        {
            string cssUrl;
            if (TreeStyleFile == string.Empty)
            {
                cssUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "VeryCodes.Controls.Web.DTree.dtree.css");
            }
            else
            {
                cssUrl = TreeStyleFile;
            }
            return cssUrl;
        }

        /// <summary>
        /// 设置描述文本
        /// </summary>
        /// <param name="deptDr">DataRow</param>
        /// <returns>描述文本</returns>
        private String SetDesciptionText(DataRow deptDr)
        {
            String DescriptionText = "";
            if (DescriptionFiled != string.Empty)
            {
                string[] DescriptionFileds = DescriptionFiled.Split(',');
                for (int k = 0; k <= DescriptionFileds.Length - 1; k++)
                {
                    string[] DescriptionFiledItem = DescriptionFileds[k].Split('|');

                    DescriptionText += DescriptionFiledItem[0].ToString() + ":" + deptDr["" + DescriptionFiledItem[1].ToString() + ""].ToString() + "<br/>";
                }

            }
            return DescriptionText;
        }

        /// <summary>
        /// 设置树节点Url
        /// </summary>
        /// <param name="deptDr"></param>
        /// <param name="i"></param>
        /// <returns></returns>
        private string SetNodeUrl(DataRow deptDr)
        {
            string NodeUrlText = NodeUrl;
            if (NodeUrlText == string.Empty)
            {
                NodeUrlText = "#";
            }
            else
            {
                string[] NodeUrls = NodeUrlText.Split('|');

                int NodeUrlsCount = NodeUrls.Length;

                if (NodeUrlsCount > 1)
                {
                    NodeUrlText = NodeUrls[0];
                    for (int k = 1; k <= NodeUrlsCount - 1; k++)
                    {
                        string UrlParams = deptDr[NodeUrls[k]].ToString();
                        string oldText = "{" + (k - 1) + "}";
                        NodeUrlText = NodeUrlText.Replace(oldText, UrlParams);
                    }
                }
            }
            return NodeUrlText;
        }

        /// <summary>
        /// 递归输出子节点
        /// </summary>
        /// <param name="output">输出内容</param>
        /// <param name="NodePK">节点pk</param>
        /// <param name="NodeCode">节点code</param>
        private void SetChildNode(HtmlTextWriter output, String NodePK, String NodeCode)
        {
            if (NodePK != "")
            {
                DataTable deptTbl = BindDataTable;
                DataRow[] deptDr = deptTbl.Select("" + ParentFiled + " = '" + NodePK + "'");


                if (deptDr.Length > 0)
                {
                    for (int i = 0; i <= deptDr.Length - 1; i++)
                    {
                        String DataItemText = SetDataItemText(deptDr[i], NodeTextFiled);

                        String DataItemValue = SetDataItemText(deptDr[i], NodeValueField);

                        //如果描述字段不为空，则显示描述内容
                        String DescriptionText = SetDesciptionText(deptDr[i]);

                        //点击节点的链接或触发事件
                        string NodeUrlText = SetNodeUrl(deptDr[i]);

                        //获取链接跳转位置
                        string NodeTargetTemp = "right";
                        if (NodeTargetField != "")
                        {
                            NodeTargetTemp = deptDr[i][NodeTargetField].ToString();
                        }

                        //是否支持单选框
                        bool isEnableCheckBox = true;
                        if (EnableCheckValue != string.Empty)
                        {
                            isEnableCheckBox = false;
                            EnableCheckValue = "," + EnableCheckValue + ",";
                            if (EnableCheckValue.Contains("," + DataItemValue + ","))
                            {
                                isEnableCheckBox = true;
                            }
                        }

                        //checkbox节点是否选中
                        bool IsNodeChecked = SetNodeChecked(deptDr[i], DataItemText, DataItemValue);

                        string nodeIcon = string.Empty;
                        if (NodeIconField != string.Empty)
                        {
                            nodeIcon = deptDr[i][NodeIconField].ToString();
                            if (nodeIcon != "") nodeIcon = _NodeIconPath + nodeIcon;
                        }

                        string nodeIconOpen = string.Empty;
                        if (NodeIconOpenField != string.Empty)
                        {
                            nodeIconOpen = deptDr[i][NodeIconOpenField].ToString();
                            if (nodeIconOpen != "") nodeIconOpen = _NodeIconPath + nodeIconOpen;
                        }

                        //树节点信息
                        output.WriteLine("d" + ClientID + ".add(\"" + deptDr[i]["" + NodeRelationField + ""].ToString() + "\",\"" + NodeCode + "\",\"" + DataItemText + "\",\"" + NodeUrlText + "\",\"" + DataItemValue + "\",\"\",\"" + DescriptionText + "\",\"" + NodeTargetTemp + "\",\"" + nodeIcon + "\",\"" + nodeIconOpen + "\"," + _AllOpen.ToString().ToLower() + "," + IsNodeChecked.ToString().ToLower() + "," + isEnableCheckBox.ToString().ToLower() + ");");


                        DataRow[] nowDr = deptTbl.Select("" + ParentFiled + " = '" + deptDr[i]["" + _NodePKField + ""] + "'");

                        if (nowDr.Length > 0)
                        {
                            SetChildNode(output, deptDr[i]["" + _NodePKField + ""].ToString(), deptDr[i]["" + NodeRelationField + ""].ToString());
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 初始化节点是否选中
        /// 执行判断顺序:
        /// 1、是否有选中字段
        /// 2、是否有选中值
        /// 3、是否有选中文本
        /// </summary>
        /// <param name="deptDr"></param>
        /// <param name="i"></param>
        /// <param name="DataItemText"></param>
        /// <returns>true or false</returns>
        private bool SetNodeChecked(DataRow deptDr, String DataItemText, String DataItemValue)
        {
            bool IsNodeChecked = false;
            if (ShowCheckBox)
            {
                if (CheckedNodeField != string.Empty)
                {
                    IsNodeChecked = bool.Parse(deptDr["" + CheckedNodeField + ""].ToString());
                }
                else if (CheckedNodeValue != string.Empty)
                {
                    string[] CheckedNodeValues = CheckedNodeValue.Split('|');
                    int CheckedNodeValuesCount = CheckedNodeValues.Length;
                    for (int k = 0; k <= CheckedNodeValuesCount - 1; k++)
                    {
                        if (CheckedNodeValues[k] == DataItemValue)
                        {
                            IsNodeChecked = true;
                            break;
                        }
                    }
                }
                else if (CheckedNodeText != string.Empty)
                {
                    string[] CheckedNodeTexts = CheckedNodeText.Split('|');
                    int CheckedNodeTextsCount = CheckedNodeTexts.Length;
                    for (int k = 0; k <= CheckedNodeTextsCount - 1; k++)
                    {
                        if (CheckedNodeTexts[k] == DataItemText)
                        {
                            IsNodeChecked = true;
                            break;
                        }
                    }
                }
            }
            return IsNodeChecked;
        }

        /// <summary>
        /// 初始化显示的节点文本
        /// </summary>
        /// <param name="deptDr"></param>
        /// <param name="NodeFiled"></param>
        /// <returns></returns>
        private String SetDataItemText(DataRow deptDr, string NodeFiled)
        {
            String DataItemText = "";
            string[] NodeFileds = NodeFiled.Split('|');
            int NodeFiledsCount = NodeFileds.Length;

            if (NodeFiledsCount > 1)
            {
                for (int k = 0; k <= NodeFiledsCount - 1; k++)
                {
                    DataItemText = DataItemText + deptDr[NodeFileds[k]].ToString() + " ";
                }
                DataItemText = DataItemText.Substring(0, DataItemText.Length - 1);
            }
            else
            {
                DataItemText = deptDr["" + NodeFiled + ""].ToString();
            }
            return DataItemText;
        }


        #endregion

        #region protected方法

        /// <summary>
        /// 输出树
        /// </summary>
        /// <param name="output"></param>
        protected virtual void RenderTree(HtmlTextWriter output)
        {
            output.WriteLine("<script type=\"text/javascript\">");

            //树的基本设置
            InitDTree(output);

            //设置树的节点
            SetNode(output);

            output.WriteLine("document.write(d" + ClientID + ");");
            output.WriteLine("</script>");
        }

        /// <summary>
        /// DTree的基本设置
        /// </summary>
        /// <param name="output"></param>
        protected void InitDTree(HtmlTextWriter output)
        {
            output.WriteLine("d" + ClientID + " = new dTree(\"d" + ClientID + "\",\"dtree_" + ClientID + "\",\"" + DescriptionTitle + "\",\"" + TreeStyleId + "\");");
            output.WriteLine("d" + ClientID + ".config.inOrder = true;");
            output.WriteLine("d" + ClientID + ".config.useCheckBox = " + ShowCheckBox.ToString().ToLower() + ";");
            output.WriteLine("d" + ClientID + ".config.useRelationParent = " + RelationParent.ToString().ToLower() + ";");
            output.WriteLine("d" + ClientID + ".config.useLines = " + UseLines.ToString().ToLower() + ";");
            output.WriteLine("d" + ClientID + ".config.useIcons = " + UseIcons.ToString().ToLower() + ";");
            output.WriteLine("d" + ClientID + ".icon.root = '" + DTreeRootImage.ToString() + "';");
            output.WriteLine("d" + ClientID + ".icon.folder = '" + DTreeFolderImage.ToString() + "';");
            output.WriteLine("d" + ClientID + ".icon.folderOpen = '" + DTreeFolderOpenImage.ToString() + "';");
            output.WriteLine("d" + ClientID + ".icon.node = '" + DTreeNodeImage.ToString() + "';");
            output.WriteLine("d" + ClientID + ".icon.empty = '" + DTreeEmptyImage.ToString() + "';");
            output.WriteLine("d" + ClientID + ".icon.line = '" + DTreeLineImage.ToString() + "';");
            output.WriteLine("d" + ClientID + ".icon.join = '" + DTreeJoinImage.ToString() + "';");
            output.WriteLine("d" + ClientID + ".icon.joinBottom = '" + DTreeJoinBottomImage.ToString() + "';");
            output.WriteLine("d" + ClientID + ".icon.plus = '" + DTreePlusImage.ToString() + "';");
            output.WriteLine("d" + ClientID + ".icon.plusBottom = '" + DTreePlusBottomImage.ToString() + "';");
            output.WriteLine("d" + ClientID + ".icon.minus = '" + DTreeMinusImage.ToString() + "';");
            output.WriteLine("d" + ClientID + ".icon.minusBottom = '" + DTreeMinusBottomImage.ToString() + "';");
            output.WriteLine("d" + ClientID + ".icon.nlPlus = '" + DTreeNoLinesPlusImage.ToString() + "';");
            output.WriteLine("d" + ClientID + ".icon.nlMinus = '" + DTreeNoLinesMinusImage.ToString() + "';");

            //设置是否显示描述文本
            if (DescriptionFiled != string.Empty)
            {
                output.WriteLine("d" + ClientID + ".config.useShowTips = true;");
            }
        }

        /// <summary>
        /// 输出树的节点
        /// </summary>
        /// <param name="output"></param>
        protected void SetNode(HtmlTextWriter output)
        {
            //设置根节点
            output.WriteLine("d" + ClientID + ".add('" + RootNodeCode + "','-1','" + RootNodeName + "');");

            //初始化树的节点
            SetChildNode(output, RootNodePK, RootNodeCode);
        }

        #endregion
    }

}